package org.khd.auth.controller;

import org.khd.auth.context.LoginContext;
import org.khd.auth.domain.Menu;
import org.khd.auth.query.MenuQuery;
import org.khd.auth.service.IMenuService;
import org.khd.base.utils.AjaxResult;
import org.khd.base.utils.PageList;
import org.khd.org.domain.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/menu")
public class MenuController {
    @Autowired
    public IMenuService menuService;


    /**
     * 保存和修改公用的
     * @param menu  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Menu menu){
        try {
            if( menu.getId()!=null)
                menuService.update(menu);
            else
                menuService.add(menu);
            return AjaxResult.ARDefault();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.ARDefault().setMsg("保存对象失败！"+e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult deleteById(@PathVariable("id") Long id){
        try {
            menuService.deleteById(id);
            return AjaxResult.ARDefault();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.ARDefault().setMsg("删除对象失败！"+e.getMessage());
        }
    }
	
    //通过id获取用户
    @GetMapping("/{id}")
    public AjaxResult queryById(@PathVariable("id")Long id)
    {
        try {
            Menu menu = menuService.queryById(id);
            return AjaxResult.ARDefault().setData(menu);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.ARDefault().setSuccess(false).setMsg("获取一个失败！"+e.getMessage());
        }
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping
    public AjaxResult queryAll(){

        try {
            List< Menu> list = menuService.queryAll();
            return AjaxResult.ARDefault().setData(list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.ARDefault().setSuccess(false).setMsg("获取所有失败！"+e.getMessage());
        }
    }

    /***
     * 查询动态菜单
     * @return
     */
    @GetMapping("/byMenu")
    public AjaxResult getByMenu(HttpServletRequest request){

        try {
            Employee employee = LoginContext.getEmployee(request);
            List< Menu> list = menuService.getByMenu(employee.getId());
            return AjaxResult.ARDefault().setData(list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.ARDefault().setSuccess(false).setMsg("获取所有失败！"+e.getMessage());
        }
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping
    public AjaxResult queryPage(@RequestBody MenuQuery query)
    {
        try {
            PageList<Menu> pageList = menuService.queryPage(query);
            return AjaxResult.ARDefault().setData(pageList);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.ARDefault().setSuccess(false).setMsg("获取分页数据失败！"+e.getMessage());
        }
    }
}
